import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:awake1/awake1.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: PublicKey(),
    );
  }
}

class PublicKey extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PublicKeyState();
  }
}

class PublicKeyState extends State<PublicKey> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> getPublicKey() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await Awake1.getPublicKey(_accountEditController.text);
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  // TextEditingController用来监听文本
  TextEditingController _accountEditController = TextEditingController();
  TextEditingController _pwdEditController = TextEditingController();

// 设置globalKey，用于后面获取FormState
  GlobalKey _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Form'),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(vertical: 40, horizontal: 20),
        child: Form(
          key: _formKey,
          autovalidate: true,
          child: Column(
            children: <Widget>[
              _buildAccountTF(),
              _buildPublicKeyText(),
              _buildLoginButton(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildAccountTF() {
    return TextField(
      autofocus: true,
      controller: _accountEditController,
      decoration: InputDecoration(
        hintText: '请输入私钥',
        contentPadding: EdgeInsets.all(10.0),
        border: OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.pink,
          ),
        ),
      ),
      maxLines: 4,
      enableInteractiveSelection: true,
      onChanged: (value) {
        _accountEditController.text = value;
      },
    );
  }

  Widget _buildPublicKeyText() {
    return Text("导入的私钥账户名字为:"+_platformVersion);
  }

  Widget _buildLoginButton() {
    return Padding(
      padding: const EdgeInsets.all(20),
      child: Row(
        children: <Widget>[
          Expanded(
              child: RaisedButton(
                  padding: const EdgeInsets.all(5),
                  child: Text(
                    '点我转换',
                    style: TextStyle(
                      fontSize: 20,
                    ),
                  ),
                  textColor: Colors.blue,
                  splashColor:Colors.red,
                  onPressed: () {
                    getPublicKey();
                  }),
          )
        ],
      ),
    );
  }
}

